package ywpt.service.config;

import java.util.ArrayList;
import java.util.List;

import ywpt.core.dao.extend.HibernateEntityExtendDao;
import ywpt.core.exception.UniqueConstraintException;
import ywpt.core.util.StringTools;
import ywpt.model.ClientInfo;
import ywpt.model.ClientKeshi;
import ywpt.model.CommonTable;
import ywpt.model.CommonTypes;
import ywpt.model.KnowledgeType;
import ywpt.web.common.PublicFunction;

import org.directwebremoting.annotations.RemoteMethod;
import org.directwebremoting.annotations.RemoteProxy;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;
@Service("commonTableService")
@RemoteProxy(name="commonTableService")
public class CommonTableServiceImpl extends HibernateEntityExtendDao<CommonTable> implements CommonTableService {

	@Override
	public String createCode(String prefixCode) {
		return null;
	}

	@Override
	@RemoteMethod
	public CommonTable getEntityById(Long id) {
		return super.get(id);
	}

	@Override
	public int getEntityCountByCriteria(DetachedCriteria query) {
		return this.getCountByCriteria(query);
	}

	@Override
	public List<CommonTable> getEntitys() {
		return super.getAll();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<CommonTable> getEntitys(DetachedCriteria query) {
		return super.findAllByCriteria(query);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<CommonTable> getEntitys(DetachedCriteria query, int pageSize,
			int startIndex) {
		return super.findAllByCriteria(query, pageSize, startIndex);
	}

	@Override
	public List<CommonTable> getEntitysByName(String name, int start, int size) {
		return null;
	}

	@Override
	public int getEntitysCountByName(String name) {
		return 0;
	}

	@Override
	public void remove(Long id) {
		CommonTable temp=this.get(id);
		if(temp!=null)
		{
			temp.setDeleteFlag(true);
		}
		this.update(temp);
		
	}

	@Override
	public void save(CommonTable commonTable) {
		super.save(commonTable);
		
	}

	@Override
	public void update(CommonTable commonTable) {
		this.onValid(commonTable);
		super.update(commonTable);
	}
	
	@Override
	protected void onValid(CommonTable entity) {
		if(!isUnique(CommonTable.class,entity,"code"))
			throw new UniqueConstraintException("编码已经存在，请从新输入！");
	}

	@SuppressWarnings("unchecked")
	@Override
	@RemoteMethod
	public List<CommonTable> getCommonTableByTypesCode(String TypesCode) {
		List<CommonTable> commonTableList = new ArrayList<CommonTable>();
		DetachedCriteria query=DetachedCriteria.forClass(CommonTable.class);
		if(!StringTools.isNullOrSpace(TypesCode))
			query.createAlias("commonTypes","commonTypes").add(Restrictions.eq("commonTypes.code",TypesCode));
		
		query.add(Restrictions.eq("deleteFlag",false));
		commonTableList.addAll(this.findAllByCriteria(query));
		return commonTableList;
	}

	@Override
	@RemoteMethod
	public List<CommonTable> select(String typesCode) {
		DetachedCriteria query=DetachedCriteria.forClass(CommonTable.class);
		if(!StringTools.isNullOrSpace(typesCode))
			query.createAlias("commonTypes","commonTypes").add(Restrictions.eq("commonTypes.code",typesCode));
		query.add(Restrictions.eq("deleteFlag",false));
		return this.getEntitys(query);	
	}

	@Override
	@RemoteMethod
	public String creatCodeByTypeId(Long typeId) {
		DetachedCriteria query = DetachedCriteria.forClass(CommonTable.class);
		String tabCode="";
		String code="";
		String typeCode="";
		Integer maxId=new Integer(0);
		if(typeId!=null){
			typeCode = this.get(CommonTypes.class,typeId).getCode();
			query.createAlias("commonTypes", "commonTypes").add(Restrictions.eq("commonTypes.id", typeId));
		}else{
			query.add(Restrictions.isNull("parent"));
		}
		query.addOrder(Order.desc("id"));
		List<CommonTable> commonTables = this.getCommonTables(query);
		if ( commonTables.iterator().hasNext() )	
		{
			code = commonTables.iterator().next().getCode().substring(typeCode.length());	
			maxId = new Integer(code);
		}
		tabCode=PublicFunction.getNextCode("", maxId, false,3); 
		return typeCode+tabCode;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<CommonTable> getCommonTables(DetachedCriteria query) {
		query.add(Restrictions.eq("deleteFlag", false));
		return this.findAllByCriteria(query);
	}

}
